
if (files.cwd().endsWith("脚本")) {
    let _require = require;
    require = function (path) {
        if (path.startsWith("./")) {
            path = path.replace("./", "");
        }
        path = files.cwd() + "/gdt_hb/" + path;
        return _require(path);
    }
}
auto.waitFor()
var utils = require("./utils.js");
var storage = storages.create("xiangxiangyuedu");

function launchAppEnhance(app_name) {
    let thread = threads.start(function () {
        toastLog("检查是否有打开权限!");
        while (true) {
            var allow = textContains("允许").findOne(5000);
            if (allow) {
                sleep(1000)
                toastLog("发现允许授权!");
                allow.click();
                sleep(1000);
                thread.interrupt();
                break;
            }
        }
    });
    sleep(1000);
    var result = launchApp(app_name);
    sleep(3000);
    thread.interrupt();
    return result;
}

function task() {
    var wakuang = id("navigation_dig").findOne(20000);
    if (wakuang) {
        wakuang.click();
        log("点击挖矿成.")
        utils.updateAppProgress(20);
    }
    sleep(8500);

    var dislike = id("tt_insert_dislike_icon_img").findOne(100000);
    if (dislike) {
        dislike.click()
    }
    sleep(1500);
    if (text("立即兑换").findOne(1000) || text("知道了").findOne(1000)) {
        var gb = depth(20).className("android.widget.Image").clickable(true).findOne(1000)
        if (gb) {
            click(gb.bounds().centerX(), gb.bounds().centerY());
            log("关闭")
            sleep(1500);
        }
    }
    if (desc("立即兑换").findOne(1000) || desc("知道了").findOne(1000)) {
        var gb = depth(20).className("android.widget.Image").clickable(true).findOnce()
        if (gb) {
            click(gb.bounds().centerX(), gb.bounds().centerY());
            log("关闭")
            sleep(1500);
        }
    }
    sleep(4000);
    utils.updateAppProgress(80);
    var sq = desc("收取BTD").findOnce()
    if (sq) {
        click(sq.bounds().centerX(), sq.bounds().centerY());
    }
    var sq = text("收取BTD").findOnce()
    if (sq) {
        click(sq.bounds().centerX(), sq.bounds().centerY());
        utils.updateAppProgress(80);
    }
    var start = text("开始挖矿").findOne(5000);
    if (start) {
        start.click()
    }
    var gb = desc("开始挖矿").findOne(1000)
    if (gb) {
        click(gb.bounds().centerX(), gb.bounds().centerY());
    }

}


function main() {
    let open_app = launchAppEnhance("BTD");
    if (open_app) {
        task()
    } else {
        toastLog("BTD打开失败，请确认是否安装并授权打开!");
    }
}

try {
    main();
    utils.updateAppProgress(100);
} catch (error) {
    toastLog(error);
}

storage.put("script_running_status", "end");
